package com.eryos.android.cigarettecounter.database;

import java.util.ArrayList;
import java.util.List;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2012 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public class SQLQuery {

	List<String> selectClause;
	List<String> whereClause;
	List<String> groupClause;
	List<String> orderClause;
	String fromClause;
	
	public SQLQuery(){
		selectClause = new ArrayList<String>();
		whereClause = new ArrayList<String>();
		groupClause = new ArrayList<String>();
		orderClause = new ArrayList<String>();
		fromClause = "FROM "+DBConnector.DATABASE_NAME+" ";
	}

	
	public void setSelect(String... selectList){
		for(String select : selectList){
			selectClause.add(select);
		}
	}
	
	public void setWhere(String... selectList){
		for(String select : selectList){
			whereClause.add(select);
		}
	}
	
	public void setOrder(String... selectList){
		for(String select : selectList){
			orderClause.add(select);
		}
	}
	
	public void setGroup(String... selectList){
		for(String select : selectList){
			groupClause.add(select);
		}
	}
	
	public String toString(){
		String select = "SELECT ";
		for (String att : selectClause )
			select += att+", ";
		select = select.substring(0, select.length()-2);

		String where = "WHERE ";
		for (String att : whereClause )
			where += att+" ";

		String order = "";
		if ( orderClause.size() > 0 ){
			order = "ORDER BY ";
			for (String att : orderClause )
				order += att+", ";
			order = order.substring(0, select.length()-2);
		}

		String group = "";
		if ( groupClause.size() > 0 ){
			group = "GROUP BY ";
			for (String att : groupClause )
				group += att+", ";
			group = group.substring(0, select.length()-2);
		}
		
		
		return select+" "+fromClause+" "+where+" "+group+" "+order+";";
	}
}
